package logic

import (
	"context"
	"fmt"
	"time"
	"zgyzk3/model/redis"

	"zgyzk3/zgyzk3rpc/internal/svc"
	"zgyzk3/zgyzk3rpc/zgyzk3rpc"

	"github.com/zeromicro/go-zero/core/logx"

	"github.com/mojocn/base64Captcha"
)

type CaptchaLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCaptchaLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CaptchaLogic {
	return &CaptchaLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CaptchaLogic) Captcha(in *zgyzk3rpc.CaptchaRequest) (*zgyzk3rpc.CaptchaResponse, error) {
	// todo: add your logic here and delete this line
	c := base64Captcha.NewCaptcha(base64Captcha.DefaultDriverDigit, base64Captcha.DefaultMemStore)
	_, m, b64s, err := c.Generate()

	if err != nil {
		return nil, fmt.Errorf("验证码图片上传失败")
	}

	redis.Client.Set("captcha"+in.Tel+in.Source, b64s, time.Minute*5)

	return &zgyzk3rpc.CaptchaResponse{Image: m}, nil
}
